const Service = require('egg').Service;
class bookService extends Service {
  /**
   * 列表
   */
  async list() {
    const { ctx } = this;
    let body = ctx.request.body;
    body = body ? body : { page: 1, size: 100, type: 1 };
    let where = {};
    if (body.name) {
      where.name = body.name;
    }
    // if (item.like) {
    //   where = {
    //     like: item.like,
    //   };
    // }
    const list = await ctx.model.Books.findAndCountAll({
      limit: body.size || 100,
      offset: (body.page - 1) * body.size,
      where,
    });
    return list;
  }
  /**
   * 详情
   */
  async detail() {
    const { ctx } = this;
    const body = ctx.request.body;
    const item = await ctx.model.Books.findOne({
      where: {
        id: body.id,
      },
    });
    return item;
  }
  /**
   * 更新参数
   */
  async update() {
    const { ctx } = this;
    const body = ctx.request.body;
    const item = await ctx.model.Books.findOne({
      where: {
        id: body.id,
      },
    });
    item.dataValues = Object.assign({}, item.dataValues, body);
    item.save();
    return item;
  }
  /**
   * 删除
   */
  async destroy() {
    const { ctx } = this;
    const body = ctx.request.body;
    const item = await ctx.model.Books.destroy({
      where: {
        id: body.id,
      },
    });
    return item;
  }
}

module.exports = bookService;
